﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MovieRental_Presentation.DataViews;
using MovieRental.EnhancedEntities;
using MovieRental_Presentation.Helpers;
using MovieRental.Entities;

namespace MovieRental_Presentation
{
	public partial class Cart
	{


		public Cart()
		{
			this.InitializeComponent();
            App app = (App) App.Current;
            DataContext = app.CartDataView;

			// Insert code required on object creation below this point.
		}

        public Page ParentPage
        {
            get
            {
                Page parentPage = null;
                var parent = VisualTreeHelper.GetParent(this); ;
                while (parentPage == null)
                {
                    parent = VisualTreeHelper.GetParent(parent);
                    if (parent is Page)
                        parentPage = (Page)parent;
                }
                return parentPage;
            }
        }

		private void acceptButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            ParentPage.NavigationService.Navigate(new LoanDialog());
		}

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            var cart = (App.Current as App).CartDataView.CurrentCart.Entity;
            var loan = (Loan)loansList.SelectedItem;
            ComponentManager.Instance.LoanCartComponent.RemoveEditionFromCart(cart, loan.Entity.EditionId);
        }

        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            var cartDataView = (App.Current as App).CartDataView;
            CustomerInfo customer = ComponentManager.Instance.CustomerComponent.Get(cartDataView.CurrentCart.Entity.CustomerId);
            cartDataView.CurrentCart = new LoanCart();
            if (customer != null)
                ComponentManager.Instance.LoanCartComponent.AssignCustomerToCart(cartDataView.CurrentCart.Entity, customer);
        }
	}
}